package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.dto.RecordDto;
import com.zhentao.mapper.ClassInstructorTeacherMapper;
import com.zhentao.mapper.ClassesMapper;
import com.zhentao.mapper.PointsRecordMapper;
import com.zhentao.mapper.StudentMapper;
import com.zhentao.pojo.ClassInstructorTeacher;
import com.zhentao.pojo.PointsRecord;
import com.zhentao.pojo.Student;
import com.zhentao.service.PointsRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author hp
* @description 针对表【points_record(积分记录表)】的数据库操作Service实现
* @createDate 2025-07-09 15:20:23
*/
@Service
public class PointsRecordServiceImpl extends ServiceImpl<PointsRecordMapper, PointsRecord>
    implements PointsRecordService{
    @Autowired
    private PointsRecordMapper pointsRecordMapper;
    @Autowired
    private ClassesMapper classesMapper;
    @Autowired
    private ClassInstructorTeacherMapper classInstructorTeacherMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public Page<PointsRecord> findRecord(RecordDto dto) {
        Page<PointsRecord> page = new Page<>(dto.getSize(),dto.getCurrent());


        QueryWrapper<ClassInstructorTeacher> classInstructorTeacherQueryWrapper = new QueryWrapper<>();
        classInstructorTeacherQueryWrapper.eq("teacher_id",dto.getTeacherId());
        List<ClassInstructorTeacher> classInstructorTeachers = classInstructorTeacherMapper.selectList(classInstructorTeacherQueryWrapper);
        List<Integer> cId = new ArrayList<>();
        for (ClassInstructorTeacher cit:classInstructorTeachers
             ) {
            cId.add(cit.getClassId());
        }
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.in("class_id",cId);
        List<Student> students = studentMapper.selectList(studentQueryWrapper);
        List<Integer> sId = new ArrayList<>();
        for (Student s:students
             ) {
            sId.add(s.getId());
        }

        QueryWrapper<PointsRecord> pointsRecordQueryWrapper = new QueryWrapper<>();
        pointsRecordQueryWrapper.in("student_id",sId);
        pointsRecordMapper.selectPage(page,pointsRecordQueryWrapper);
        List<PointsRecord> records = page.getRecords();
        if(records!=null&&records.size()>0){
            return page;
        }
        return null;
    }
}




